home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / $Print_MENU_Resource < prev    next >
Encoding:
Text File  |  1993-04-09  |  5.2 KB  |  193 lines  |  [TEXT/KEEN]

  1. #$Print_MENU_Resource - use with MENU retrieved by Read Resource.
  2. #Use "Read Resource" to retrieve the MENU resource you want
  3. #- it will be shown in stdout. Leave stdout showing, and then use
  4. #"hAWK" to run this program, taking input from "All of front text".
  5. #Note hAWK will take the text from stdout, and then write its output
  6. #to stdout - sort of like piping.
  7.  
  8. #Display a MENU resource, reformatted for easier reading
  9. #- an example of how to use Read Resource followed by a simple
  10. #hAWK program to display your own custom resources - handy to
  11. #confirm/display the contents of a resource during development
  12. #
  13. #The approach below uses "input on demand" to allow us
  14. #to step through the bytes in the resource. Each call to
  15. #GetNextByte or PrintCharAndAdvance retrieves the next byte, grabbing
  16. #the next input line if necessary. The variable bytePos is
  17. #automatically advanced whenever a byte is read, and the calculation
  18. #of the field number and position within the field is hidden within
  19. #GetNextByte. To skip over n bytes, add n to bytePos.
  20. #
  21. #MENU resource format: Inside Mac vol 1 pg 364
  22. #num bytes        contents
  23. #14                header info
  24. #1                length of menu title
  25. #n                characters of title
  26. #--for each menu item:
  27. #1                length of following text in bytes
  28. #m                text of menu item
  29. #4                icon num, keyboard equiv, mark char, style
  30. #-- null byte at very end
  31. #
  32. #Format of resource as retrieved by Read Resource:
  33. #byteNumber: 4bytes 4bytes 4bytes 4bytes  ascii version of the bytes
  34. #the last line may be padded with zeroes, for example
  35. #      240: 07556E6D 61726BC9 00000000 09417574  .Unmark......Aut
  36. #      256: 6F6D6172 6BC90000 00000000 00000000  omark......
  37.  
  38. #bytePos ranges 0 : up. It is automatically
  39. #advanced when a byte is read by GetNextByte(). It can be
  40. #manually advanced to skip unwanted bytes.
  41.  
  42. # User’s Manual references:
  43. # «hAWK User’s Manual» «F   Running hAWK programs»
  44. # «hAWK User’s Manual» «L  5   Regular expressions»
  45. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  46. # «hAWK User’s Manual» «K  4   Built-in variables»
  47. # «hAWK User’s Manual» «K  8   Arrays»
  48. # «hAWK User’s Manual» «N   User-defined functions»
  49. # «hAWK User’s Manual» «P  3   The getline function»
  50. # «hAWK User’s Manual» «O  3   Output into files»
  51. # «hAWK User’s Manual» «Q   The hAWK function»
  52.  
  53. FNR == 1    {bytePos = 14; #Skip header info.
  54.             printf("Menu: ");
  55.             menuLen = GetNextByte(); #Length of menu title.
  56.             if (menuLen <= 0)#No name - get right to the items.
  57.                 {
  58.                 printf("(no name)\n\n");
  59.                 }
  60.             else
  61.                 {
  62.                 printf("%3d  ", menuLen);
  63.                 while (menuLen > 0)
  64.                     {
  65.                     PrintCharAndAdvance();
  66.                     --menuLen;
  67.                     }
  68.                 printf("\n\n");
  69.                 }
  70.             printf("Len  Item text\n");
  71.             printf("---  ---------\n");
  72.             
  73.             while ((itemLen = GetNextByte()) > 0)
  74.                 {
  75.                 printf("%3d  ", itemLen);
  76.                 while (itemLen > 0)
  77.                     {
  78.                     PrintCharAndAdvance();
  79.                     --itemLen;
  80.                     }
  81.                 print ""
  82.                 #Skip icon num, keyboard equiv, mark char, style.
  83.                 bytePos += 4;
  84.                 }
  85.             }
  86.  
  87.  
  88.  
  89. function PrintCharAndAdvance()
  90.     {
  91.     printf("%c", GetNextByte());
  92.     }
  93.  
  94. #Convert ASCII representation of next byte, eg '1F', to a single
  95. number, eg 31.
  96. function GetNextByte(        lineByte, field, char, pos)
  97.     {
  98.     #Convert byte position in resource to field and position in field.
  99.     lineByte = bytePos - (FNR - 1) * 16;
  100.     field = int((lineByte)/4);
  101.     while (field > 3)#the next line of input is wanted
  102.         {
  103.         if ((getline) < 0)
  104.             {
  105.             print ""; print "End of file."; exit;
  106.             }
  107.         lineByte = bytePos - (FNR - 1) * 16;
  108.         field = int((lineByte)/4);
  109.         }
  110.     pos = lineByte - field * 4;
  111.     field += 2;
  112.     pos = 2 * pos + 1;
  113.     char = HexToDec(substr($field, pos, 2));
  114.     ++bytePos;
  115.     return char
  116.     }
  117.  
  118. #Return raw ASCII representation of byte, eg "2F". Not used in
  119. #this program.
  120. function GetNextRawByte(        lineByte, field, char, pos)
  121.     {
  122.     #Convert byte position in resource to field and position in field.
  123.     lineByte = bytePos - (FNR - 1) * 16;
  124.     field = int((lineByte)/4);
  125.     while (field > 3)#the next line of input is wanted
  126.         {
  127.         if (getline < 0)
  128.             {
  129.             print ""; print "End of file."; exit;
  130.             }
  131.         lineByte = bytePos - (FNR - 1) * 16;
  132.         field = int((lineByte)/4);
  133.         }
  134.     pos = lineByte - field * 4;
  135.     field += 2;
  136.     pos = 2 * pos + 1;
  137.     ++bytePos;
  138.     return substr($field, pos, 2);
  139.     }
  140.  
  141. #Convert ASCII representation of 1, 2, or 4 bytes to a single
  142. #unsigned number. Not used in this program.
  143. function GetNextBytesAsNumber(howManyBytes,        i, temp, numString, number)
  144.     {
  145.     for (i = 1; i <= howManyBytes; ++i)
  146.         {
  147.         temp = GetNextRawByte();
  148.         numString = numString temp;
  149.         }
  150.     number = HexToDec(numString);
  151.     return number;
  152.     }
  153.  
  154. #General purpose ASCII hex to number conversion.
  155. function HexToDec(h,     len, nyb, hexDigit, power, d)
  156.     {
  157.     d = 0;
  158.     len = length(h);
  159.     power = 1
  160.     for (nyb = len; nyb >= 1; --nyb)
  161.         {
  162.         hexDigit = substr(h, nyb, 1);
  163.         d += DecimalEquiv(hexDigit) * power;
  164.         power *= 16;
  165.         }
  166.     return d
  167.     }
  168.  
  169. #Convert one hex "digit" to the equivalent number.
  170. function DecimalEquiv(hd, dd)
  171.     {
  172.     dd = 0;
  173.     if (match(hd,/[0-9]/) > 0)
  174.         dd = hd + 0;#conversion from string to number is implicit
  175.     else if (match(hd, /[a-fA-F]/) > 0)
  176.         {
  177.         if (match(hd, /[aA]/) > 0)
  178.             dd = 10;
  179.         else if (match(hd, /[bB]/) > 0)
  180.             dd = 11;
  181.         else if (match(hd, /[cC]/) > 0)
  182.             dd = 12;
  183.         else if (match(hd, /[dD]/) > 0)
  184.             dd = 13;
  185.         else if (match(hd, /[eE]/) > 0)
  186.             dd = 14;
  187.         else if (match(hd, /[fF]/) > 0)
  188.             dd = 15;
  189.         }
  190.     return dd
  191.     }
  192.  
  193.